home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr44 / xlib06p1.zip / XPLAIN.CPP < prev    next >
C/C++ Source or Header  |  1995-03-15  |  6KB  |  231 lines

  1. #include "xinternl.h"
  2. #include <conio.h>
  3. #include <mem.h>
  4.  
  5. /*==================================================================
  6. XPLAIN.CPP contains the basic functions for pixel-resolution collision
  7. detection in mode X.
  8.  
  9. These routines were written initially by Tiaan A Geldenhuys and
  10. company and modified March 1995 by Victor B. Putz.
  11. ===================================================================*/
  12.  
  13. extern xScreenCoord_t TopClip;
  14. extern xScreenCoord_t BottomClip;
  15.  
  16. int CollideX;
  17. int CollideY;
  18.  
  19. void x_pbm_to_plain(         /* Convert from planar bitmap to plain bitmap */
  20.   BYTE * pbSource,
  21.   BYTE * pbDest,
  22.   xColor_t ColorMask
  23. )
  24. {
  25.   int iNibbleWidth = *pbSource++;
  26.   int iHeight = *pbSource++;
  27.   //round up our nibble width to get byte width
  28.   int iByteWidth = ( iNibbleWidth + 1 ) / 2;
  29.   *pbDest++ = ( BYTE )iByteWidth;
  30.   *pbDest++ = ( BYTE )iHeight;
  31.   //store the location of the bitmap, since we come to it every plane
  32.   BYTE * pbDestRef = pbDest;
  33.   memset( pbDest, 0, iByteWidth * iHeight );
  34.   BYTE bHighPixMask = 0x80;
  35.   BYTE bLowPixMask = 0x08;
  36.   //now copy the pixels to bits
  37.   for ( int iPlaneCounter = 0; iPlaneCounter < 4; ++iPlaneCounter ) {
  38.     pbDest = pbDestRef;
  39.     for ( int iHeightCounter = 0; iHeightCounter < iHeight; ++iHeightCounter ) {
  40.       for ( int iWidthCounter = 0; iWidthCounter < iNibbleWidth; ++iWidthCounter ) {
  41.     int iTest = *pbSource++;
  42.     if ( iTest & ColorMask ) {
  43.       *pbDest |= bHighPixMask;
  44.     }
  45.     ++iWidthCounter;
  46.     if ( iWidthCounter < iNibbleWidth ) {
  47.       iTest = *pbSource++;
  48.       if ( iTest & ColorMask ) {
  49.         *pbDest |= bLowPixMask;
  50.       }
  51.     }
  52.     ++pbDest;
  53.       }
  54.     }
  55.     bHighPixMask >>= 1;
  56.     bLowPixMask >>= 1;
  57.   }
  58.  
  59. }
  60.  
  61.  
  62.  
  63. char x_check_collision_plain(
  64.   BYTE *VicStruct1,
  65.   int X1,
  66.   int Y1,
  67.   BYTE *VicStruct2,
  68.   int X2,
  69.   int Y2
  70. )
  71. {
  72.     //rearrange so that VicStruct1 is Leftmost
  73.   if ( X1 > X2 ) {
  74.     int iTemp = X2;
  75.     X2 = X1;
  76.     X1 = iTemp;
  77.     iTemp = Y2;
  78.     Y2 = Y1;
  79.     Y1 = iTemp;
  80.     BYTE * pbTemp = VicStruct2;
  81.     VicStruct2 = VicStruct1;
  82.     VicStruct1 = pbTemp;
  83.   }
  84.   //now that we're assured VicStruct1 is leftmost, get width and height
  85.   int iWidth1 = *VicStruct1++;
  86.   int iHeight1 = *VicStruct1++;
  87.   int iWidth2 = *VicStruct2++;
  88.   int iHeight2 = *VicStruct2++;
  89.   //First, just check for a rectangular collision
  90.   //get the offset into the first map of the second map
  91.   int iXOffset = X2 - X1;
  92.   //we know there's a collision; now let's turn the X offset from a pixel
  93.   //offset to a byte offset and get a "right shift" to see how many
  94.   //bits to the right we need to shift VicStruct1's data
  95.   int iRightShift = iXOffset & 0x07;
  96. //  iXOffset &= 0xfff8;
  97.     iXOffset >>= 3;
  98.   //if we're past the first map, return
  99.   if ( iXOffset > iWidth1 ) {
  100.     return 0;
  101.   }
  102.   //now check the Y part
  103.   int iYOffset = Y2 - Y1;
  104.   if ( iYOffset > iHeight1 ) {
  105.     return 0;
  106.   }
  107.   if ( ( iYOffset + iHeight2 ) < 0 ) {
  108.     return 0;
  109.   }
  110.  
  111.   if ( iYOffset < 0 ) {
  112.     iYOffset *= -1;
  113.   }
  114.   //Well, now we get to the nitty gritty pixel-based detection.
  115.   //this will be messy code, because I don't have time to do it well.
  116.   //in most cases, the rectangular collision detection will reduce
  117.   //the time necessary.  If it becomes an issue I'll look at it.
  118.   int iXToCheck = iWidth1 - iXOffset;
  119.   if ( iXToCheck > iWidth2 ) {
  120.     iXToCheck = iWidth2;
  121.   }
  122.   int iSkip1 = iWidth1 - iXToCheck;
  123.   int iSkip2 = iWidth2 - iXToCheck;
  124.  
  125.   int iYToCheck = 0;
  126.   BYTE * pb1 = VicStruct1;
  127.   BYTE * pb2 = VicStruct2;
  128.   if ( Y1 <= Y2 ) {
  129.     iYToCheck = iHeight1 - iYOffset;
  130.     if ( iYToCheck > iHeight2 ) {
  131.       iYToCheck = iHeight2;
  132.     }
  133.     pb1 += iYOffset * iWidth1 + iXOffset;
  134.     pb2 += 0;
  135.   }
  136.   else {
  137.     iYToCheck = iHeight2 - iYOffset;
  138.     if ( iYToCheck > iHeight1 ) {
  139.       iYToCheck = iHeight1;
  140.     }
  141.     pb1 += iXOffset;
  142.     pb2 += iYOffset * iWidth2;
  143.   }
  144.   while( iYToCheck-- ) {
  145.         int iCheck1 = 0;
  146.     int iCheck2 = 0;
  147.     int iXCount = iXToCheck;
  148.     while( iXCount-- ) {
  149.       iCheck1 += *pb1++;
  150.       iCheck2 += *pb2++;
  151.       if ( ( iCheck1 << iRightShift ) & iCheck2 ) {
  152.                 return 1;
  153.       }
  154.       iCheck1 <<= 8;
  155.       iCheck2 <<= 8;
  156.     }
  157.     pb1 += iSkip1;
  158.     pb2 += iSkip2;
  159.   }
  160.   return 0;
  161. }
  162.  
  163.  
  164.  
  165. BYTE abFlipTable[] = {
  166.   0x00, 0x08, 0x04, 0x0c, 0x02, 0x0a, 0x06, 0x0e,
  167.     0x01, 0x09, 0x05, 0x0d, 0x03, 0x0b, 0x07, 0x0f
  168. };
  169.  
  170. extern BYTE * pbVGABuffer;
  171. extern int ScrnLogicalByteWidth;
  172. extern BYTE abMirrorTable[];
  173. extern WORD awTextMask[];
  174. /*
  175. Copy a VIC bitmap (variable size) from SRAM to VRAM.
  176.  
  177. Clips in Y direction only, and does not clip both top and bottom ( ie if
  178. the top is clipped, bottom is not ).
  179. */
  180. void x_put_plain(
  181.   int X,
  182.   int Y,
  183.   xPageHandle_t ScrnOffs,
  184.   BYTE * VicStruct,
  185.   xColor_t Color
  186. )
  187. {
  188.   BYTE * pbDest = pbVGABuffer + ScrnOffs + ( ScrnLogicalByteWidth * Y ) + X / 4;
  189.   BYTE * pbSource = VicStruct;
  190.   int iByteWidth = *pbSource++;
  191.   int iHeight = *pbSource++;
  192.   int iSkip = ScrnLogicalByteWidth - ( iByteWidth * 2 );
  193.   //clip in Y direction
  194.   //clip top
  195.   int iHeightToPut = iHeight;
  196.   if ( Y < TopClip ) {
  197.     iHeightToPut = ( Y + iHeight ) - TopClip;
  198.     if ( iHeightToPut <= 0 ) {
  199.       return;
  200.     }
  201.     //now update pbSource to look at the new start
  202.     pbSource += ( TopClip - Y ) * iSkip;
  203.     pbDest = pbVGABuffer + ScrnOffs + ( ScrnLogicalByteWidth * TopClip ) + X / 4;
  204.   }
  205.   else if ( ( Y + iHeight ) > BottomClip ) {
  206.     iHeightToPut = BottomClip - Y;
  207.     if ( iHeightToPut <= 0 ) {
  208.       return;
  209.     }
  210.   }
  211.   int iRotation = ( X & 3 );
  212.   outp( SC_INDEX, MAP_MASK );
  213.   int iTemp = 0;
  214.   for( int iRowCounter = 0; iRowCounter < iHeightToPut; ++iRowCounter ) {
  215.     for ( int iColumnCounter = 0; iColumnCounter < iByteWidth; ++iColumnCounter ) {
  216.       iTemp = *pbSource++;
  217.       iTemp = abMirrorTable[ iTemp ];
  218.       iTemp <<= iRotation;
  219.           //TEST STUFF!
  220.       outpw( SC_INDEX, awTextMask[ iTemp & 0x0f ] );
  221.       *pbDest++ = ( BYTE )Color;
  222.       outpw( SC_INDEX, awTextMask[ ( iTemp & 0xf0 ) >> 4 ] );
  223.       *pbDest++ = ( BYTE )Color;
  224.           outpw( SC_INDEX, awTextMask[ iTemp >> 8 ] );
  225.       *pbDest = ( BYTE )Color;
  226.     }
  227.     pbDest += iSkip;
  228.   }
  229. }
  230.  
  231.